package kr.ne.jay.SimpleString;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;

public class StringThread extends Thread {
	private SurfaceHolder m_surfaceHolder;
	private SimString	m_simString;
	private Paint		m_paint  = new Paint();
	private boolean		m_done = false;

	public StringThread(SurfaceHolder _holder, SimString _string) {
		super("String simulation thread");
		m_surfaceHolder = _holder;
		m_simString	= _string;
	}

	public void setLineColor(int color) {
		m_paint.setColor(color);
	}

	public void doDraw(Canvas canvas) {
		canvas.drawColor(Color.WHITE);
		canvas.drawLines(m_simString.getLines(), m_paint);
		//canvas.drawPath(m_simString.getPath(), m_paint);
	}

	@Override
	public void run() {
		while (!m_done) {
			Canvas c = null;
			try {
                c = m_surfaceHolder.lockCanvas(null);
                synchronized (m_surfaceHolder) {
					m_simString.update();
					if (c != null)
						doDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    m_surfaceHolder.unlockCanvasAndPost(c);
                }
            }
		}
	}
	
	public void requestExitAndWait() {
		m_done = true;
		try {
			join();
		} catch (InterruptedException e) {
			// ignore
		}
	}
}
